import {reqAddressInfo, reqShopInfo, reqSubmitOrder} from '@/api/index.js';

// 材料：
let state = {
    address: [],
    tradeInfo: {},
    payId: ''
};

// 加工：
let getters = {};

// 厨师：
let mutations = {
    SET_ADDRESS(state, address) {
        state.address = address;
    },
    SET_TRADE_INFO(state, tradeInfo) {
        state.tradeInfo = tradeInfo;
    },
    SET_PAY_ID(state, payId) {
        state.payId = payId;
    }
};

// 迎宾：
let actions = {
    // 获取用户信息地址
    async getAddress({commit, state, dispatch}) {
        console.log(200);
        let result = await reqAddressInfo();
        console.log(result);
        if (result.code === 200) {
            commit('SET_ADDRESS', result.data);
            return result;
        } else {
            return Promise.reject(new Error(result.message));
        }
    },
    // 获取商品清单
    async getShopInfo({commit, state, dispatch}) {
        console.log(300);
        let result = await reqShopInfo();
        console.log(result);
        if (result.code === 200) {
            commit('SET_TRADE_INFO', result.data);
            return result;
        } else {
            return Promise.reject(new Error(result.message));
        }
    },
    // 提交订单（tradeNO交易编码，data付款人信息）
    async submitInfo({commit, state, dispatch}, {tradeNo, data}) {
        let result = await reqSubmitOrder(tradeNo, data);
        if (result.code === 200) {
            commit('SET_PAY_ID', result.data);
            return result;
        } else {
            return Promise.reject(new Error(result.message));
        }
    }
};

// 对外暴露
export default {
    state,
    mutations,
    actions,
    getters
};
